var nowdepid;
var nowclass;

//获取单位详细信息
function depdetail(x,y){
	if (y>200 || (x==0 && y!=200)){
		var url = "frontend/depdetail.asp?x="+x+"&y="+y+"&timestamp="+ new Date().getTime();
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handledetailStateChange;
    	xmlHttp.open("get",url,true);
    	xmlHttp.send(null);
		document.all.topoperation.innerHTML = "查看";
		if (x==1){
			var b = document.getElementById("b"+y);
			var c = document.getElementById("b"+nowdepid);
			if(c!=null){
				c.className = nowclass;
			}
			if(b!=null){
				nowclass = b.className;
				b.className="nowfocus";
				nowdepid=y;
			}
		}else{
			var c = document.getElementById("b"+nowdepid);
			if(c!=null){
				c.className = nowclass;
				nowdepid=null;
			}
		}
	}
}

function handledetailStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
			var ajaxRequestObj = xmlHttp.responseText;
			var c = ajaxRequestObj.split("|")
			addtag(c[2],c[1],"nonedata.gif");
			document.getElementById(c[2]).innerHTML = c[0];
			switchtag(c[2]);
        }
    }
}

function handleStateChange1() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
		var ajaxRequestObj = xmlHttp.responseText;
		shadeandoperate();
		document.getElementById(nowdiv+"_operate").innerHTML = ajaxRequestObj;
        }
    }
}
//创建遮罩层和显示执行层
function shadeandoperate(c){
	if(document.getElementById(nowdiv+"_shade")==null){
		var maindiv = document.getElementById(nowdiv);
		var theshade = CreateEl("div",nowdiv+"_shade","shade");
		theshade.style.height=maindiv.clientHeight-70;
		theshade.style.width=maindiv.clientWidth;
		theshade.style.posTop=35;
		theshade.style.posLeft=0;
		maindiv.appendChild(theshade);
		var theoperate = CreateEl("div",nowdiv+"_operate","operate");
		if(c==1){
			theoperate.style.height=maindiv.clientHeight-70;
		}
		theoperate.style.posTop=35;
		theoperate.style.posLeft=0;
		maindiv.appendChild(theoperate);
	}
}
//删除遮罩
function removeshadeandoperate(){
	var maindiv = document.getElementById(nowdiv);
	menuobj = document.getElementById(nowdiv+"_menu");
	document.getElementById(nowdiv+"_shade").removeNode(true);
	document.getElementById(nowdiv+"_operate").removeNode(true);
	if(menuobj!=null){
		menuobj.style.background="";
		menuobj.id="";
	}
	document.getElementById(nowtag).getElementsByTagName("img")[0].src="/javascripts/dep_manage/images/nonedata.gif";
}

function fover(){
 var e=event.srcElement;
 if(e.tagName=="TD" && e.parentNode.id!="select_tr"){
  e.parentNode.style.backgroundColor="#eefbea";
  }
 if((e.tagName=="A" || e.tagName=="IMG")&& e.parentNode.parentNode.parentNode.id!="select_tr"){
  e.parentNode.parentNode.parentNode.style.backgroundColor="#e7ffdf";
  }
}
function fout(){
 var e=event.srcElement;
 if(e.tagName=="TD" && e.parentNode.id!="select_tr"){
  e.parentNode.style.backgroundColor="#FFFFFF,#FFFFFF".split(",")[e.parentNode.rowIndex%2];
  }
 if((e.tagName=="A" || e.tagName=="IMG")&& e.parentNode.parentNode.parentNode.id!="select_tr"){
  e.parentNode.parentNode.parentNode.style.backgroundColor="#FFFFFF,#FFFFFF".split(",")[e.parentNode.parentNode.parentNode.rowIndex%2];
  }
}
function fclick(){
 var e=event.srcElement;
 var f=document.getElementById("select_tr");
 if(f!=null){
   f.style.backgroundColor="#fff";
   f.removeAttribute("id");
  }
 if(e.tagName=="TD"){
  e.parentNode.id="select_tr";
  e.parentNode.style.backgroundColor="#c3d7c5";
  }
 if(e.tagName=="A" || e.tagName=="IMG"){
  e.parentNode.parentNode.parentNode.id="select_tr";
  e.parentNode.parentNode.parentNode.style.backgroundColor="#b9dcbc";
  }
}

//标签相关鼠标事件函数
var menufocus=false;
var menuobj=null;
function addbk(e){
	e.style.background="url(/javascripts/dep_manage/images/onhover.gif) no-repeat";
}
function movebk(e){
	e.style.background="";
}
function downbk(e){
	e.style.background="url(/javascripts/dep_manage/images/onclick.gif) no-repeat";
}

function initmenuobj(){
	if(document.getElementById(nowdiv+"tag").getElementsByTagName("img")[0].src="/javascripts/dep_manage/images/nonedata_edit.gif"){
		menufocus=true;
		var imgs=document.getElementById(nowdiv+"tag").getElementById("manage_menu").getElementsByTagName("img");
		for(var i=0;i<imgs.length-1;i++){
			//if(imgs[i].style.backgroudImage="url()")
		}
	}else{
		menufocus=false;
		menuobj=null;
	}
}

function menuaddbk(e){
	//initmenuobj();
	if(e.id!=nowdiv+"_menu"){
		e.style.background="url(/javascripts/dep_manage/images/bigbtn_hover.gif) no-repeat";
	}
	genhints(e.getElementsByTagName("img")[0].src.split("btn_")[1].split(".")[0]);
}
//功能提示
function genhints(s){
	switch(s){
		case "01":genhint("修改当前单位的注册信息");break;
		case "02":genhint("移动当前单位的注册部委或上级单位");break;
		case "03":genhint("合并当前单位到其他单位");break;
		case "04":genhint("在当前部委或单位下，新增下级单位");break;
		case "05":genhint("删除当前单位，并冻结当前单位注册用户");break;
		case "06":genhint("查看当前部委或单位采购统计情况");break;
		case "07":genhint("打印当前单位及用户信息");break;
		case "11":genhint("查看当前部委大额项目申报情况");break;
		case "12":genhint("委托本单位大额采购项目");break;
		case "21":genhint("查看当前部委采购计划情况");break;
		case "22":genhint("申报本单位计划");break;
		case "31":genhint("设置当前单位为本部委本级单位");break;
		case "32":genhint("取消当前单位的本部委本级单位设置");break;
		case "41":genhint("锁定当前单位或下级单位，其他用户无法对这些单位进行操作");break;
		case "42":genhint("解锁当前单位，其他用户可以对当前单位进行操作");break;
		case "43":genhint("禁止当前单位或部委的用户注册");break;
		case "44":genhint("允许当前单位或部委的用户注册");break;
		default:break;
	}	
}
function menumovebk(e){
	if(e.id!==nowdiv+"_menu"){
		e.style.background="";
	}
}
function menudownbk(e){
	menuobj = document.getElementById(nowdiv+"_menu");
	if(menuobj==null){
		e.style.background="url(/javascripts/dep_manage/images/bigbtn_onclick.gif) no-repeat";
		e.id=nowdiv+"_menu";
		document.getElementById(nowtag).getElementsByTagName("img")[0].src="/javascripts/dep_manage/images/nonedata_edit.gif";
	}else{
		if(e.id!=nowdiv+"_menu"){
			e.style.background="url(/javascripts/dep_manage/images/bigbtn_onclick.gif) no-repeat";
			menuobj.style.background="";
			menuobj.id="";
			e.id=nowdiv+"_menu";
		}else{
			e.style.background="url(/javascripts/dep_manage/images/bigbtn_onclick.gif) no-repeat";
		}
	}
}
function menuupbk(e){
	e.style.background="url(/javascripts/dep_manage/images/bigbtn_focus.gif) no-repeat";
}

//标签列表按钮相关鼠标事件函数
var showtablist = false;
function tablistaddbk(e){
	e.style.background="url(/javascripts/images/images/tabchange_11.jpg) no-repeat";
	e.getElementsByTagName("div")[1].style.background="url(/javascripts/dep_manage/images/tabchange_12.jpg) no-repeat";
}
function tablistmovebk(e){
	e.style.background="";
	e.getElementsByTagName("div")[1].style.background="";
}
function tablistdownbk(e){
	e.style.background="url(/javascripts/dep_manage/images/tabchange_21.jpg) no-repeat";
	e.getElementsByTagName("div")[1].style.background="url(/javascripts/images/images/tabchange_22.jpg) no-repeat";
}
function tablistupbk(e){
	e.style.background="url(/javascripts/dep_manage/images/tabchange_11.jpg) no-repeat";
	e.getElementsByTagName("div")[1].style.background="url(/javascripts/dep_manage/images/tabchange_12.jpg) no-repeat";
}

function showtaglist(e){
	showtablist = true;
	if(document.getElementById("showtablist")!=null){
		hidemenuie5();
	}
	addtltobody();
	setTimeout("setonclick()",60);
	showtablist = false;
}
function setonclick(){
	document.body.onclick = hidemenuie5;
	//alert(document.body.onclick);
}
function addtltobody(){
	var tl = document.getElementById("tablist");
	var stl = CreateEl("div","showtablist","showtablist");
	var stlbg = CreateEl("div","showtablistbg","showtablistbg");
	stl.onmouseover=new Function("highlighttl();");
	stl.onmouseout=new Function("lowlighttl();");
	var tags = document.getElementById("tag").getElementsByTagName("span");
	for (var i=tags.length;i>0;i--){
		addsonlist(stl,tags,i);
		if(i==2){
			var x=document.createElement("hr");
			x.style.height=1;
			x.style.margin=0;
			x.style.marginLeft=-2;
			x.color="#cacaca";
			stl.appendChild(x);
		}
	}
	document.getElementsByTagName("body")[0].appendChild(stl);
	stlbg.style.height=document.getElementById("showtablist").clientHeight;
	document.getElementsByTagName("body")[0].appendChild(stlbg);
}
function addsonlist(up,alltag,i){
	var t = alltag[i-1];
	var tid = t.id.split("tag")[0];
	//alert(tid);
	var s=CreateEl("div",tid+"_list","listitems");
	s.onclick=new Function("switchtag('"+tid+"')");
	if(tid!=nowdiv||tid=="firstpage"){
		s.innerHTML=t.innerHTML;
	}else{
		s.innerHTML=t.innerHTML.split("<DIV id=close_tab>")[0];
	}
	up.appendChild(s);
}

//是否锁定修改权限
function changerightboxstate(){
	var thediv = document.getElementById(nowdiv);
	var rb = thediv.getElementsByTagName("input")[0];
	var abcd = thediv.getElementsByTagName("table")[1];
	var t = abcd.getElementsByTagName("tr");
	var s;
	if (rb.checked){
		for (var i=0;i<t.length;i++){
			s = t[i].getElementsByTagName("img");
			if(userlevel<4){
				s[1].src="/javascripts/dep_manage/images/select_"+s[1].src.split("_")[1].split(".")[0]+".gif";
				s[2].src="/javascripts/dep_manage/images/select_"+s[2].src.split("_")[1].split(".")[0]+".gif";
				if(userlevel<3){
					s[3].src="/javascripts/dep_manage/images/select_"+s[3].src.split("_")[1].split(".")[0]+".gif";
					if(userlevel<2){
						s[4].src="/javascripts/dep_manage/images/select_"+s[4].src.split("_")[1].split(".")[0]+".gif";
					}
				}
			}
			addhint(true);
		}
		addrollback(true);
	}else{
		for (var i=0;i<t.length;i++){
			s = t[i].getElementsByTagName("img");
			if(userlevel<4){
				s[1].src="/javascripts/dep_manage/images/state_"+s[1].src.split("_")[1].split(".")[0]+".gif";
				s[2].src="/javascripts/dep_manage/images/state_"+s[2].src.split("_")[1].split(".")[0]+".gif";
				if(userlevel<3){
					s[3].src="/javascripts/dep_manage/images/state_"+s[3].src.split("_")[1].split(".")[0]+".gif";
					if(userlevel<2){
						s[4].src="/javascripts/dep_manage/images/state_"+s[4].src.split("_")[1].split(".")[0]+".gif";
					}
				}
			}
			addhint(false);
		}
		addrollback(false);
	}
}
//修改权限
function chargebox(e){
	var thediv = document.getElementById(nowdiv);
	var rb = thediv.getElementsByTagName("input")[0];
	if (rb.checked){
		var eid = e.id.split("s");
		var esrc = e.src.split("_"+eid[1])[1].split(".")[0];
		var opt = Math.abs(esrc-1);
		var url = "backend/charge.asp?timestamp="+ new Date().getTime();
		var poststring = "id="+eid[0]+"&item="+eid[1]+"&state="+esrc;
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = new Function("chargeboxStateChange('"+eid[0]+"','"+eid[1]+"')");
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlHttp.send(poststring);
		document.all.topoperation.innerHTML = "权限";
		rb.parentNode.getElementsByTagName("input")[1].value += ","+eid[0]+"|"+eid[1]+"|"+opt;
	}
}
//回滚权限操作
function rollbackall(id){
	var thediv = document.getElementById(nowdiv);
	var list = thediv.getElementsByTagName("input")[1].value.split(",");
	var l = list.length;
	if(l==1){
		alert("没有可回滚的权限操作！");
	}else{
		if(confirm("确实要撤销所有的权限操作么？")){
			for (var i=l-1;i>0;i--){
				setTimeout("dorollback('"+list[i]+"')",366*(l-i));
			}
			var thediv = document.getElementById(nowdiv);
			thediv.getElementsByTagName("input")[1].value="rollbacklist";
			setTimeout("alert(\"回滚操作完毕！\");",366*l);
		}
	}
}
function dorollback(s){
	var thediv = document.getElementById(nowdiv);
	var rb = thediv.getElementsByTagName("input")[0];
	if (rb.checked){
		var e = s.split("|");
		var url = "backend/charge.asp?timestamp="+ new Date().getTime();
		var poststring = "id="+e[0]+"&item="+e[1]+"&state="+e[2];
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = new Function("chargeboxStateChange('"+e[0]+"','"+e[1]+"')");
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
		xmlHttp.send(poststring);
		document.all.topoperation.innerHTML = "权限";
	}
}

function chargeboxStateChange(id,it){
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
			var ajaxRequestObj = xmlHttp.responseText;
			var imgid = id+"s"+it;
			var thediv = document.getElementById(nowdiv);
			var rb = thediv.getElementsByTagName("input")[0];
			if (rb.checked){
				if(ajaxRequestObj!="err"){
					r = ajaxRequestObj.split("|");
					document.getElementById(imgid).src="/javascripts/dep_manage/images/select_"+it+r[0]+".gif";
					var ex = r[0]+r[1];
					//alert(ex);
					switch (ex){
						case "0a":
							genhint("撤销\""+r[3]+"\"的\""+r[4]+"\"部委管理员权限");break;
						case "1a":
							genhint("把\""+r[3]+"\"设置为\""+r[4]+"\"的部委管理员");break;
						case "0b":
							genhint("撤销\""+r[3]+"\"的\""+r[4]+"\"单位管理员权限");break;
						case "1b":
							genhint("把\""+r[3]+"\"设置为\""+r[4]+"\"的单位管理员");break;
						case "0c":
							genhint("取消\""+r[3]+"\"的\""+r[4]+"\"部委统计功能");break;
						case "1c":
							genhint("设置\""+r[3]+"\"的\""+r[4]+"\"部委统计功能");break;
						case "0d":
							genhint("取消\""+r[3]+"\"的\""+r[4]+"\"单位统计功能");break;
						case "1d":;
							genhint("设置\""+r[3]+"\"的\""+r[4]+"\"单位统计功能");break;
						default:break;
					}
					
				}
			}
        }
    }
}
//状态设置函数
function genhint(c){
	var depid = nowdiv.split("p")[1];
	document.getElementById("manage_bottom"+depid).innerHTML="	<div align=\"left\"><img src=\"/javascripts/dep_manage/images/hint_info.gif\" align=\"absmiddle\">&nbsp;"+c+"</div>";
}
//增加状态栏权限说明
function addhint(add){
	var depid = nowdiv.split("p")[1];
	var h=document.getElementById("manage_bottom"+depid);
	if(add){
		h.innerHTML="	<div align=\"left\"><img src=\"/javascripts/images/images/hint_info.gif\" align=\"absmiddle\">&nbsp;权限符号提示：<img id=\"sa2\" src=\"/javascripts/dep_manage/images/state_a1.gif\" align=\"absmiddle\" />&nbsp;部委管理&nbsp;&nbsp;<img id=\"sb2\" src=\"/javascripts/dep_manage/images/state_b1.gif\" align=\"absmiddle\" />&nbsp;单位管理&nbsp;&nbsp;<img id=\"sc2\" src=\"/javascripts/dep_manage/images/state_c1.gif\" align=\"absmiddle\" />&nbsp;部委统计&nbsp;&nbsp;<img id=\"sd2\" src=\"/javascripts/dep_manage/images/state_d1.gif\" align=\"absmiddle\" />&nbsp;单位统计&nbsp;</div>";
	}else{
		h.innerHTML="";
	}
}
//增加回滚按钮
function addrollback(add){
	var thediv = document.getElementById(nowdiv);
	var rb = thediv.getElementsByTagName("input")[0];
	var depid = nowdiv.split("p")[1];
	if(add){
		rb.parentNode.innerHTML=rb.parentNode.innerHTML+"<img src=\"/javascripts/dep_manage/images/rollback.gif\" align=\"absmiddle\" title=\"撤销已完成权限操作\" onclick=\"rollbackall('"+depid+"');\" style=\"margin-left:5px;cursor:pointer\">";
		addrollbacklist(true);
	}else{
		rb.parentNode.removeChild(rb.parentNode.getElementsByTagName("img")[0]);
		addrollbacklist(false);
	}
}
//增加回滚list
function addrollbacklist(add){
	var thediv = document.getElementById(nowdiv);
	var rb = thediv.getElementsByTagName("input")[0];
	if(add){
		var i = document.createElement("input");
		i.type="hidden";
		i.value="rollbacklist";
		rb.parentNode.appendChild(i);
	}else{
		//alert(rb.parentNode.getElementsByTagName("input")[1].value);
		rb.parentNode.removeChild(rb.parentNode.getElementsByTagName("input")[1]);
	}
}

function copyToClipboard(txt) {
	if(window.clipboardData) {
		window.clipboardData.clearData();
		window.clipboardData.setData("Text", txt);
		alert("复制到剪贴板成功！");
	} else if(navigator.userAgent.indexOf("Opera") != -1) {
		window.location = txt;
	} else if (window.netscape) {
		try {
		netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
		} catch (e) {
			alert("您的firefox安全限制限制您进行剪贴板操作，请打开'about:config'将signed.applets.codebase_principal_support'设置为true'之后重试");
			return false;
		}
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip)
			return;
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans)
			return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext = txt;
		str.data = copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip)
			return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
}

function tellmetable(){
	var t = document.getElementById(nowdiv).getElementsByTagName("div");
	var ts = "";
	for(var i=0;i<t.length;i++){
		ts += t[i].id+"|";
	}
	alert(ts);
}
